package com.bainuo.doctor.common.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.NinePatch;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import com.bainuo.doctor.common.R;

/**
 * Created by bainuo on 2018/3/21.
 */

public class CustomProgressTextView extends CustomTextView{


    private int progress = 0;
    private int color = 0;
    private boolean drawColor;

    public CustomProgressTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomProgressTextView(Context context) {
        super(context);
    }

    public void setProgress(int progress){
        this.progress = progress;
        invalidate();
    }

//    private int imgId;
//    public void setImagebackground(int id){
//        imgId = (id == 0)?R.drawable.image_wdxsmf:id;
//    }


    public void setBGColor(int color) {
//        super.setBackgroundColor(color);
        this.color = color;
        drawColor = true;
    }

    @Override
    protected void onDraw(Canvas canvas) {

        if (drawColor){

            if (getWidth() == 0) return;
            int width = getWidth();
            int height = getHeight();
            int r = height/2;

//            Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
//            Canvas cv = new Canvas(bitmap);
//
//
//            Bitmap bt = BitmapFactory.decodeResource(getResources(), imgId);
//            NinePatch ninePatch = new NinePatch(bt, bt.getNinePatchChunk(), null);
//            RectF rectF = new RectF(0, 0, getWidth(), getHeight());
//            ninePatch.draw(canvas, rectF);
//            bt.recycle();





            Paint paint = new Paint();
            paint.setColor(color);
            canvas.drawRoundRect(new RectF(0,0,width,height),r,r,paint);
            canvas.drawRect(new RectF(0,r,r,height),paint);


            paint.setARGB(50,255,255,255);
            canvas.drawRect(new Rect(0,0,getWidth()*progress/100,getHeight()),paint);
            super.onDraw(canvas);

//            canvas.drawBitmap(bitmap,0,0,new Paint());
//
//            bitmap.recycle();

        }else {
            super.onDraw(canvas);
        }




//        bt = BitmapFactory.decodeResource(getResources(), R.drawable.icon_yymrgreenl);
//        ninePatch = new NinePatch(bt, bt.getNinePatchChunk(), null);
//        rectF = new RectF(0, 0, getWidth(), getHeight());
//        ninePatch.draw(cv, rectF);
//        bt.recycle();


//        Paint paint = new Paint();
//        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
//        cv.drawRect(new Rect(getWidth()*progress/100,0,getWidth(),getHeight()),paint);



    }


}
